package pl.kosmowska.ewelina.client.views.widgets.grids;

import java.util.Comparator;
import java.util.Date;

import pl.kosmowska.ewelina.client.app.requestfactory.dto.LoanDataProxy;
import pl.kosmowska.ewelina.client.views.impl.beans.Installment;

import com.google.gwt.cell.client.DateCell;
import com.google.gwt.cell.client.NumberCell;
import com.google.gwt.dom.client.Style.Unit;
import com.google.gwt.user.cellview.client.Column;
import com.google.gwt.user.cellview.client.ColumnSortEvent.ListHandler;
import com.google.gwt.user.cellview.client.DataGrid;
import com.google.gwt.view.client.DefaultSelectionEventManager;
import com.google.gwt.view.client.ProvidesKey;
import com.google.gwt.view.client.SelectionChangeEvent;
import com.google.gwt.view.client.SingleSelectionModel;

public class InstallmentGrid<T> extends PagingDataGrid<T>{
    final ProvidesKey<T> KEY_PROVIDER = new ProvidesKey<T>() {
        public Object getKey(T item) {
          return item == null ? null : ((Installment)item).getId();
        }
      };
	
	final SingleSelectionModel<T> selectionModel =new SingleSelectionModel<T>(KEY_PROVIDER);
	
	T selectedItem =null;
	
	public T getSelectedItem() {
		return selectedItem;
	}

	@Override
	public void initTableColumns(DataGrid<T> dataGrid,
			ListHandler<T> sortHandler) {
		
	    final ProvidesKey<T> KEY_PROVIDER = new ProvidesKey<T>() {
	        public Object getKey(T item) {
	          return item == null ? null : ((Installment)item).getId();
	        }
	      };
	    // Add a selection model so we can select cells.
	     final SingleSelectionModel<T> selectionModel =new SingleSelectionModel<T>(KEY_PROVIDER);
	    selectionModel.addSelectionChangeHandler(new SelectionChangeEvent.Handler() {

	        @Override
	        public void onSelectionChange(SelectionChangeEvent event) {
	            selectedItem = selectionModel.getSelectedObject();
	        }
	    });
	    dataGrid.setSelectionModel(selectionModel, DefaultSelectionEventManager
	            .<T> createDefaultManager());
	   
		//Value column
		Column<T, Number> numberColumn = new Column<T, Number>(new NumberCell()) {
			@Override
			public Number getValue(T object) {
				return ((Installment) object).getId();
			}
		};
		numberColumn.setSortable(true);
		dataGrid.addColumn(numberColumn, "Nr");
		dataGrid.setColumnWidth(numberColumn, 5, Unit.PCT);

		//Value column
		Column<T, Number> capitalColumn = new Column<T, Number>(new NumberCell()) {
			@Override
			public Number getValue(T object) {
				return ((Installment) object).getCapital();
			}
		};
		capitalColumn.setSortable(false);
		dataGrid.addColumn(capitalColumn, "Kapital");
		dataGrid.setColumnWidth(capitalColumn, 5, Unit.PCT);
		
		//Value column
		Column<T, Number> interestColumn = new Column<T, Number>(new NumberCell()) {
			@Override
			public Number getValue(T object) {
				return ((Installment) object).getInterest();
			}
		};
		interestColumn.setSortable(false);
		dataGrid.addColumn(interestColumn, "Odsetki");
		dataGrid.setColumnWidth(interestColumn, 5, Unit.PCT);
		
		//Value column
		Column<T, Number> installmentColumn = new Column<T, Number>(new NumberCell()) {
			@Override
			public Number getValue(T object) {
				return (((Installment) object).getCapital()+((Installment) object).getInterest());
			}
		};
		interestColumn.setSortable(false);
		dataGrid.addColumn(installmentColumn, "Rata");
		dataGrid.setColumnWidth(installmentColumn, 5, Unit.PCT);
		
		
		// Date column
		Column<T, Date> dateStartColumn = new Column<T, Date>(new DateCell()) {
			@Override
			public Date getValue(T object) {
				return ((Installment) object).getDate();
			}
		};
		dateStartColumn.setSortable(true);
		sortHandler.setComparator(dateStartColumn, new Comparator<T>() {
			public int compare(T o1, T o2) {
				return ((LoanDataProxy) o1).getStartDate().compareTo(
						((LoanDataProxy) o2).getStartDate());
			}
		});
		dataGrid.addColumn(dateStartColumn, "Data platnosci");
		dataGrid.setColumnWidth(dateStartColumn,10, Unit.PCT);
		
	}
	
	public SingleSelectionModel<T> getSelectionModel (){
		return selectionModel;
	}

}

